package com.example.administrator.kaohedemo.Activity;

import android.database.Cursor;
import android.os.Handler;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.kaohedemo.Action.Actions;
import com.example.administrator.kaohedemo.Action.SeeClassRecordAction;
import com.example.administrator.kaohedemo.Adapter.SeeClassRecordAdapter;
import com.example.administrator.kaohedemo.Model.SeeClassRecordAdapterBean;
import com.example.administrator.kaohedemo.R;
import com.example.administrator.kaohedemo.Store.SeeClassRecordStore;
import com.example.administrator.kaohedemo.Store.Store;
import com.example.administrator.kaohedemo.Utils.HttpUtils;
import com.squareup.otto.Subscribe;

/**
 * Created by Administrator on 2016/6/24.
 */
public class SeeClassRecordActivity extends BaseActivity {

    private TextView title_left;
    private SeeClassRecordAction seeClassRecordAction;
    private SeeClassRecordAdapter adapter;

    @Override
    protected Store getStore() {
        return SeeClassRecordStore.getInstense();
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_seeclassrecord);
        seeClassRecordAction=SeeClassRecordAction.getInstense();
        title_left= (TextView) findViewById(R.id.title_left);
        TextView title= (TextView) findViewById(R.id.title);
        title.setText("课程列表");

        RecyclerView seeclassrecordlist= (RecyclerView) findViewById(R.id.seeclassrecordlist);
        GridLayoutManager manager=new GridLayoutManager(this,2);
        manager.setOrientation(OrientationHelper.VERTICAL);
        seeclassrecordlist.setLayoutManager(manager);
        adapter=new SeeClassRecordAdapter(this);
        seeclassrecordlist.setAdapter(adapter);
    }

    @Override
    protected void initListener() {
        title_left.setOnClickListener(this);
    }

    Handler handler=new Handler();
    @Override
    protected void initData() {
        new Thread(){
            @Override
            public void run() {
                super.run();
                final String result= HttpUtils.getInstense().getResult("http://mobile.yanxiu.com/test/api/guopei/course/list?w=3&os=android&pid=1289&ver=1.7.1&pcode=010110000&token=97ed20f93925c5ed9746551c915d0578");
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        seeClassRecordAction.SendResultFromHttp(Actions.SeeClassRecordActivity.TYPE_HTTP_RESULT,Actions.SeeClassRecordActivity.KEY_HTTP_RESULT,result);
                    }
                });
            }
        }.start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_left:
                this.finish();
                break;
        }
    }

    @Subscribe
    public void SeeClassRecordStoreChanged(SeeClassRecordStore.SeeClassRecordStoreChanged ss){
        String type=ss.getType();
        switch (type){
            case Actions.CourseListActions.TYPE_HTTP_RESULT:
                adapter.setList(ss.getBean().body.modules);
                break;
            case Actions.SeeClassRecordActivity.TYPE_RECORD_CLICK:
                SeeClassRecordAdapterBean adapterbean= ss.getAdapterbean();
                Toast.makeText(this,adapterbean.getCourse_title(),Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
